package com.nulstudio.sppm.Controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.nulstudio.sppm.Annotation.ManagerJwt;
import com.nulstudio.sppm.Annotation.NormalJwt;
import com.nulstudio.sppm.Entities.UserActionInfo;
import com.nulstudio.sppm.Entities.UserInfo;
import com.nulstudio.sppm.Service.UserInfoService;
import com.nulstudio.sppm.Utils.JwtUtils;
import com.nulstudio.sppm.Utils.RequestUtils;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin(value = "*")
@RequestMapping("/service/user")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private HttpServletRequest request;

    @NormalJwt
    @PostMapping(value="/fetchAll",produces = "application/json;charset=UTF-8" )
    public String getAllUsers() {
        JSONObject json = new JSONObject();
        List<UserInfo> userInfoList = userInfoService.getAllUsers();
        final List<JSONObject> result = new ArrayList<>();
        json.put("code", 0);
        json.put("message", "");
        json.put("version", 1);
        for (final UserInfo info : userInfoList) {
            final JSONObject object = new JSONObject();
            object.put("id", info.getUid());
            object.put("name", info.getrName());
            object.put("userName", info.getuName());
            object.put("gender", info.getGender().equals("m") ? "male" : "female");
            object.put("authority", info.getAuthority());
            result.add(object);
        }
        json.put("result", result);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value="/getUserById",produces = "application/json;charset=UTF-8" )
    public String selectUserById(@RequestBody Map<String,Object> map){
        JSONObject json = new JSONObject();
        final String uid = (String)map.get("uid");
        UserInfo user = userInfoService.getInfoByUid(uid);
        json.put("code", 0);
        json.put("message", user);
        json.put("version", 1);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value="/deleteEntry",produces = "application/json;charset=UTF-8" )
    public String deleteUserById(@RequestBody Map<String, Object> map) {
        JSONObject json = new JSONObject();
        final List<Integer> uid = (List<Integer>) map.get("id");
        for (int id : uid) {
            userInfoService.deleteUser(String.valueOf(id));
        }
        json.put("code", 0);
        json.put("message", "");
        json.put("version", 1);
        return json.toJSONString();
    }
    @NormalJwt
    @PostMapping(value="/createEntry",produces = "application/json;charset=UTF-8")
    public String addUser(@RequestBody Map<String, Object> params) {
        final UserInfo userInfo = new UserInfo(
                0,
                (String) params.get("userName"),
                (String) params.get("name"),
                params.get("gender").equals("male") ? "m" : "f",
                (String) params.get("authority"),
                "",
                "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"
        );
        userInfoService.addUserInfo(userInfo);
        JSONObject json = new JSONObject(), result = new JSONObject();
        result.put("id", userInfoService.getLastId());
        json.put("code", 0);
        json.put("message", "");
        json.put("version", 1);
        json.put("result", result);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value="/userInfo",produces =  "application/json;charset=UTF-8")
    public String getInfo(){
        JSONObject json = new JSONObject();
        final String token = RequestUtils.getCurrentToken(request);
        final String uid = JwtUtils.getUserId(token);
        UserInfo userInfo = userInfoService.getInfoByUid(uid);
        json.put("code", 0);
        json.put("message", "");
        json.put("version", 1);
        JSONObject result = new JSONObject();
        result.put("userName", userInfo.getuName());
        result.put("name", userInfo.getrName());
        result.put("gender", userInfo.getGender().equals("m") ? "male" : "female");
        result.put("post", userInfo.getAuthority());
        result.put("uid", userInfo.getUid());
        json.put("result", result);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value="/modifyEntry",produces =  "application/json;charset=UTF-8")
    public String modifyInfo(@RequestBody Map<String, Object> params) {
        JSONObject json = new JSONObject();
        final UserInfo userInfo = new UserInfo(
                (Integer) params.get("id"),
                (String) params.get("userName"),
                (String) params.get("name"),
                params.get("gender").equals("male") ? "m" : "f",
                (String) params.get("authority"),
                "",
                null
        );
        userInfoService.modifyUserInfo(userInfo);
        final JSONObject result = new JSONObject();
        result.put("id", userInfo.getUid());
        json.put("code", 0);
        json.put("message", "");
        json.put("result", result);
        json.put("version", 1);
        return json.toJSONString();
    }
}
